﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Condimar.Entities;

namespace Condimar.Repository
{
    public class VendedorDAO:MasterRepository, IVendedorDAO
    {
        public List<Vendedor> GetAllFromVendedores()
        {
            var query = from c in context.Vendedors
                        select c;
            return query.ToList();
        }

        public List<Vendedor> GetFromVendedorByNombres(string nombres)
        {
            if (nombres != "")
            {
                var query = from c in context.Vendedors
                            where c.Nombres.ToUpper().Contains(nombres.ToUpper())||c.Apellidos.ToUpper().Contains(nombres.ToUpper())
                            select c;
                return query.ToList();
            }
            else
            {
                var query1 = from c in context.Vendedors
                             select c;
                return query1.ToList();
            }
        }

        public List<Vendedor> GetFromVendedorByDNI(string dni)
        {
            if (dni != "")
            {
                var query = from c in context.Vendedors
                            where c.Dni.ToUpper().Contains(dni.ToUpper())
                            select c;
                return query.ToList();
            }
            else
            {
                var query1 = from c in context.Vendedors
                             select c;
                return query1.ToList();
            }
        }

        public Vendedor GetFromVendedorById(int id)
        {
            return context.Vendedors.Find(id);
        }

        public void AddVendedor(Vendedor vendedor)
        {
            context.Vendedors.Add(vendedor);
            context.SaveChanges();
        }

        public void UpdateVendedor(Vendedor vendedor)
        {
            context.Entry(vendedor).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveVendedor(int id)
        {
            var existe = from c in context.Vendedors
                         where c.Id.Equals(id)
                         select c;

            context.Vendedors.Remove(existe.SingleOrDefault());
            context.SaveChanges();
        }
    }
}
